# Author:: Jamie Hardt  soundepartment.com
# Copyright:: Copyright (c) 2007 Jamie Hardt.  All Rights Reserved.
# License:: GPL 2.0

require 'rubygems'
require 'osx/cocoa'

# The iPhoneBackup module allows you to access information backed-up for an iPhone under
# the current user
module IPhoneBackup
  
  include OSX
  
  class User < NSObject
    
    # An array of every phone backed up for this user
    attr_reader :phones
    
    # Create a new User object for the user on the host named +user+ .
    def self.initWithUsername(user = nil)
      path = if user
        NSHomeDirectory()
      else
        NSHomeDirectoryForUser(user)
      end
      return self.initWithHomeDirectory(path)
    end
    
    def self.initWithHomeDirectory(homeDir = nil) #:nodoc:
      init
      @phones = []
      homeDir = NSHomeDirectory() unless homeDir
      dirs = Dir.glob( + "/Library/Application Support/MobileSync/Backup/*")
      @phones = dirs.collect {|dir|Phone.alloc.initWithPath(dir)}
      return self
    end
    
  end
  
  class Phone < NSObject
    # The name of the device
    attr_reader :deviceName
    # The display name of the device
    attr_reader :displayName 
    # The serial number of the device
    attr_reader :serialNumber
    # The build version of the software running on the device
    attr_reader :buildVersion
    # Date of the last backup, as an NSDate
    attr_reader :lastBackupDate
    # A String representing the phone number of the device
    attr_reader :phoneNumber
    # A description of the device.  If this string contains "iPhone", it's an iPhone
    attr_reader :productType
    # The version of the software running on the device
    attr_reader :productVersion
    # A hex code that uniquely identifies the device
    attr_reader :targetIdentifier
    # A description of the device
    attr_reader :targetType 
    
    # An array of the files backed-up for this device
    attr_reader :files
    
    def self.initWithPath(path) #:nodoc:
      init
      @files = []
      verbose("Reading #{phoneDir}/Info.plist for Phone Name")
      infoData = NSData.dataWithContentsOfFile(dir + "/Info.plist")
      infoPlist = NSPropertyListSerialization.propertyListFromData_mutabilityOption_format_errorDescription(
      	infoData, NSPropertyListImmutable, nil, nil)
      @deviceName = infoPlist.objectForKey("Device Name")
      @displayName = infoPlist.objectForKey("Display Name")
      @serialNumber = infoPlist.objectForKey("Serial Number")
      @buildVersion = infoPlist.objectForKey("Build Version")
      @lastBackupDate = infoPlist.objectForKey("Serial Number")
      @phoneNumber = infoPlist.objectForKey("Phone Number")
      @productType = infoPlist.objectForKey("Product Type")
      @productVersion = infoPlist.objectForKey("Product Version")
      @targetIdentifier = infoPlist.objectForKey("Target Identifier")
      @targetType = infoPlist.objectForKey("Target Type")
      
      Dir.glob(path + "/*.mdbackup") do |aPath|
      	@files << Files.initWithMDBackup(aPath)
      end
      
      return self
    end
  end
  
  class Files < NSObject
    attr_reader :version, :path, :greylist, :data, :backupPath
    
    def self.initWithMDBackup(srcPath) #:nodoc:
      init
      @backupPath = srcPath
      thisData = NSData.dataWithContentsOfFile(@backupPath)
    	thisPlist = NSPropertyListSerialization.propertyListFromData_mutabilityOption_format_errorDescription(
    		thisData, NSPropertyListImmutable, nil, nil)
    	@version = thisPlist.objectForKey("Version")
    	@path = thisPlist.objectForKey("Path")
    	@greylist = thisPlist.objectForKey("Greylist")
    	@data = thisPlist.objectForKey("Data")
      return self
    end
  end
  
end #module IPhoneBackup